package service

import (
	"github.com/gin-gonic/gin"
	"goCmdb/internal/modules"
	"net/http"
)

func AssetAdd(c *gin.Context) {
	Asset := modules.AssetInventory{}
	err := c.BindJSON(&Asset)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{})
		return
	}
	if Asset.AssetAdd() {
		c.JSON(http.StatusOK, gin.H{
			"user":    Asset.User,
			"address": Asset.Address,
			"message": "hosts 添加成功",
			"status":  "successfully",
		})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "hosts 已存在或者添加过程出现错误"})
	}
}

func AssetDelete(c *gin.Context) {
	Asset := modules.AssetInventory{}
	err := c.BindJSON(&Asset)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{})
		return
	}
	if Asset.AssetDelete() {
		c.JSON(http.StatusOK, gin.H{
			"user":    Asset.User,
			"address": Asset.Address,
			"message": "hosts 删除成功",
			"status":  "successfully",
		})
	} else {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "hosts 删除失败"})
	}
}

func AssetUpdate(c *gin.Context) {
	Asset := modules.AssetInventory{}
	_ = c.BindJSON(&Asset)
	if Asset.AssetUpdate(){
		c.JSON(http.StatusOK, gin.H{"message": "更新成功"})
	}else {
		c.JSON(http.StatusInternalServerError, gin.H{"message": "更新失败"})
	}
}

func AssetGet(c *gin.Context) {
	Asset := modules.AssetInventory{}
	data,err := Asset.AssetGet()
	if err != nil{
		c.JSON(http.StatusInternalServerError, gin.H{"data":"nil","message": err})
	}else {
		c.JSON(http.StatusOK, gin.H{"data":&data,"message": "successfully"})
	}
}
